﻿
var myUtils = new Object();

myUtils.format = function() {
    var s = arguments[0];
    for (var i = 0; i < arguments.length - 1; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        s = s.replace(reg, arguments[i + 1]);
    }
    return s;
}

myUtils.urlDecode = function(url) {
    if (url.lastIndexOf("?") >= 0) {
        url = url.substring(url.lastIndexOf("?") + 1);
        var obj = {};
        var pairs = url.split('&');
        for (var i = 0; i < pairs.length; i++) {
            var pair = pairs[i].split("=");
            obj[pair[0]] = pair[1];
        }
        return obj;
    } else {
        return '';
    }
}
myUtils.urlEncode = function(json) {
    var tmps = [];
    for (var key in json) {
        tmps.push(key + '=' + json[key]);
    }
    return tmps.join('&');
}

myUtils.formatTime = function(val, f) {
    var re = /-?\d+/;
    var m = re.exec(val);
    var d = new Date(parseInt(m[0]));
    return d.format(f);
}

Date.prototype.format = function(format) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

myUtils.loadIndex = function() {
    $("#header").loadPage("header.htm");
    $("#menu").loadPage("menu.htm");

    var urlPar = myUtils.urlDecode(location.href);
    if (urlPar == '') {
        $("#viewer").loadPage("viewer.htm");
        $("#content").loadPage("content.htm");
    } else {
        var cUrl = unescape(urlPar.content);
        $("#content").loadPage(cUrl);
    }
    $("#footer").loadPage("footer.htm");
}